/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ttt.nor2.importdata.txt;

import com.ttt.nor2.util.ExceptionUtil;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class ImportOver1Year extends ImportBilling {

    private Logger logger = Logger.getLogger(ImportOver1Year.class);

    @Override
    public void savedata() {
        //call function manage month
        try {
            logger.debug("row = " + row);
            logger.debug("find strint 'Sum' result : " + line.indexOf("Sum"));
            logger.debug("find strint 'Count' result : " + line.indexOf("Count"));

            if (row != 1 && row != 2 && line.indexOf("Sum") < 0 && line.indexOf("Count") < 0) {
                // find cust_id from customer param rent_id
                long cust_id = getCustid(rent_month[3]);
                // manage table phone #param phoneno, office_desc,  status,  province_id
                long phone_id = mtbPhone(rent_month[4], rent_month[1], rent_month[2], rent_month[1].substring(2, 4));
                //manage table billing
                mtbBilling(1, rent_month[3], cust_id, phone_id);
                logger.info(row + ".) Save data sucess.. done[" + rent_month[4] + "]");
            }
        } catch (Exception e) {
            logger.error("Exception call fucntion error " + ExceptionUtil.getStackTrace(e));
        } finally {
            row++;
        }// end finally
    }// end function savedata

    public int getCustid(String rent_id){
        int cust_id = 0;
        try{
            String sql = "SELECT b.cust_id FROM billings AS b WHERE b.rent_id = ? ";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, rent_id);
            ResultSet  rs = pstmt.executeQuery();
            if(rs.next()){
                cust_id = rs.getInt("cust_id");
            }
            rs.close();
            pstmt.close();
        }catch(Exception e){
            logger.error("Exception call fucntion error " + ExceptionUtil.getStackTrace(e));
        }

        return cust_id;
    }
}
